//
//  XPCountDown.m
//  DoulinClient
//
//  Created by YLKJ on 2019/1/8.
//

#import "XPCountDown.h"

@interface XPCountDown ()
@property (nonatomic, strong) NSMutableArray * blkAry;
@property (nonatomic, assign) NSInteger time;
@end

@implementation XPCountDown
SingletonM(XPCountDown)

- (void)observeTime:(NSInteger)time type:(XPCountDownType)type blk:(TimeBlk)blk {
    self.time = time;
    
    if (self.blkAry.count <= 0) {
        [kNotifyCenter addObserver:self selector:@selector(countDownNotification:) name:OYCountDownNotification object:nil];
    }
    if (type == XPCountDownTypeSingle) {
        [self.blkAry removeAllObjects];
        if (self.blkAry.count <= 0) {
            [self.blkAry addObject:blk];
        }
    } else {
        [self.blkAry addObject:blk];
    }
    
    [[OYCountDownManager manager] start];
    [[OYCountDownManager manager] reload];
}
- (void)countDownNotification:(NSNotification *)note {
    NSInteger tmp = self.time - [OYCountDownManager manager].timeInterval;
//    NSLog(@"<-- %s, %d --> %ld", __FUNCTION__, __LINE__, (long)tmp);
    for (TimeBlk blk in self.blkAry) {
        if (blk) {
            blk(tmp);
        }
    }
    if (tmp == 0) {
        [self.blkAry removeAllObjects];
        [kNotifyCenter removeObserver:self];
//        [[OYCountDownManager manager] invalidate];
//        [OYCountDownManager destroy];
//        [XPCountDown destroyXPCountDown];
    }
}

#pragma mark - ------------------ <#note#> ------------------
- (NSMutableArray *)blkAry {
    if (!_blkAry) {
        self.blkAry = [@[] mutableCopy];
    }
    return _blkAry;
}

@end
